package cn.net.autocode.platform.customDict.dict;

import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.core.utils.TreeUtils;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.platform.customDict.CustomDict;
import cn.net.autocode.platform.customDict.CustomDictAnnotation;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@CustomDictAnnotation("funcCategory")
@Component("autocode_platform_funcCategory")
public class FuncCategory implements CustomDict {
    private final Dao dictDao;

    public FuncCategory(@Qualifier("dictDao") Dao dictDao) {
        this.dictDao = dictDao;
    }


    @Override
    public ResultT query(JSONObject param) {
        JSONArray arrays = new JSONArray();
        JSONArray categorys = dictDao.queryJSONArray("select * from ac_category where type='category' order by code");
        for(int i=0;i<categorys.size();i++){
            JSONObject category = categorys.getJSONObject(i);
            category.put("id","category_"+category.getString("code"));
            category.put("parentId", "0");
            category.put("value",category.getString("code"));
        }
        arrays.addAll(categorys);
        JSONArray applications = dictDao.queryJSONArray("select * from ac_category where type='application' order by code");
        arrays.addAll(applications);
        int len=applications.size();
        for(int i=0;i<len;i++){
            JSONObject application = applications.getJSONObject(i);
            application.put("id","application_"+application.getString("code"));
            application.put("parentId","category_"+application.getString("parentCode"));
            application.put("label",application.getString("code"));
            application.put("value",application.getString("code"));
        }
        return ResultT.success(TreeUtils.commonTree(arrays));
    }
}
